package com.jinyi.wwdeng.ui.main.impl

import android.content.Context
import com.hjq.permissions.XXPermissions
import com.jinyi.base.mvp.BasePresenter
import com.jinyi.wwdeng.AppConfigs
import com.jinyi.wwdeng.network.ApiService
import com.jinyi.wwdeng.network.HttpBaseParams.addCommonParams
import com.jinyi.wwdeng.ui.main.contract.CreditLimitContract
import org.json.JSONObject

/**
 * JinYiTest
 * @Author wwdeng
 * @CreateTime: 2024/4/28 16:21
 * @Annotation：Presenter 实现类
 */
class CreditLimitPresenterImpl: BasePresenter<CreditLimitContract.View,CreditLimitModelImpl>(),CreditLimitContract.Presenter {

    //所有权限列表
    private var allPermissionList: ArrayList<String>? = null

    override fun createModel(): CreditLimitModelImpl {
        return CreditLimitModelImpl(this)
    }

    /**
     * 获取用户贷款额度
     */
    fun loadCreditLimitRequest(){
        if (isViewDestory()) {
            return
        }
        val params = JSONObject().addCommonParams().apply {
            put("path", ApiService.API_PATH.API_PATH_CREDIT_LIMIT)
        }
        model!!.loadCreditLimitRequest(params)
    }

    /**
     * 获取所有权限
     */
    fun loadAllPermissionList(){
        if (isViewDestory()) {
            return
        }
        model!!.loadPermissionList()
    }

    /**
     * 获取贷款额度成功
     */
    override fun onCreditLimitSuccess(data: JSONObject) {
        if (isViewDestory()) {
            return
        }
        when(data.optInt("code")){
            0 -> {//成功
                val obj = data.optJSONObject("obj")
                //需要显示的金额JSON
                var amountObject: JSONObject? = null
                //未完善资料之前，显示额度从启动页配置数据中取
                val commonData = JSONObject(AppConfigs.getSPString(AppConfigs.SP_KEY.SP_HENECREDIT_COMMON))
                val customize = commonData.optJSONObject("customize")
                if (customize != null){
                    amountObject = customize.optJSONObject("loanLimit")
                }
                //后台没有配置loanLimit 默认额度
                if (amountObject == null){
                    amountObject = JSONObject()
                    amountObject.put("amount1","35000")
                    amountObject.put("amount2","15000")
                    amountObject.put("amount3","20000")
                }

                //完善资料之后显示的可申请金额需要从贷款额度接口中取 amount
                val limitArray = obj!!.optJSONArray("limit")
                if (limitArray != null && limitArray.length() > 0){
                    val limitObj = limitArray.optJSONObject(0)
                    amountObject.put("amount",limitObj.optString("amount"))
                }

                mvpView!!.showCreditLimitData(amountObject)
            }
            else -> {//失败
                mvpView!!.showToast(data.optString("msg"))
            }
        }
    }

    /**
     * 获取所有权限成功
     */
    override fun onPermissionListSuccess(list: ArrayList<String>) {
        if (allPermissionList == null){
            allPermissionList = arrayListOf()
        }
        allPermissionList!!.clear()
        allPermissionList!!.addAll(list)
    }

    /**
     * 获取全部权限
     */
    fun getAllPermissionList(): ArrayList<String>{
        return allPermissionList!!
    }

    /**
     * 是否全部已授权
     */
    fun isAgreeAll(): Boolean{
        var isAll = true
        if (allPermissionList != null){
            for (i in 0..<allPermissionList!!.size){
                if (!XXPermissions.isGranted(context!!,allPermissionList!![i])){
                    isAll = false
                    break
                }
            }
        }
        return isAll
    }

    /**
     * 接口请求出错
     */
    override fun onRequestError(errorMsg: String) {
        mvpView!!.showToast(errorMsg)
    }

    override fun onRequestFinish() {
        mvpView!!.onRequestFinish()
    }

    override val context: Context?
        get() = if (!isViewDestory()) {
            mvpView!!.getContext()
        } else null
}